import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.system.JmeContext;
import com.jme3.system.JmeContext.Type;

public class ServerMain extends SimpleApplication implements ActionListener {
	Server server;
	Level level;
	private BulletAppState bulletAppState;
	
	public static void main(String[] args) {
		ServerMain app= new ServerMain();
		app.setShowSettings(false); 
		app.start(JmeContext.Type.Display);
	}
	
	public void simpleInitApp() {
		initkeys();
		flyCam.setEnabled(true);
		bulletAppState = new BulletAppState();
	    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
	    stateManager.attach(bulletAppState);
	    createLight();
	    setupFilter();
	    level = new Level(assetManager, bulletAppState);
	    rootNode.attachChild(level.makeSpaceStation());
	}
	private void initkeys() {
		inputManager.addMapping("C", new KeyTrigger(keyInput.KEY_SPACE));       
		inputManager.addMapping("D", new KeyTrigger(keyInput.KEY_D));          
		inputManager.addListener(this, new String[]{"C","D"});
	}
	@Override
	public void onAction(String name, boolean keyPressed, float arg2) {
		if (name.equals("C")&&keyPressed) {
			try {
				System.out.println("create Server pressed");
				server= Network.createServer(5000);
				server.start();
			} catch (Exception e) {
				System.out.println("server add exception");
				e.printStackTrace();
			}
		}
		if (name.equals("D")&&keyPressed) {
			try {
				if (server.isRunning()) {
					System.out.println("destroy pressed");
					server.close();
					
				}
			} catch (Exception e) {
				System.out.println("server remove exception");
				e.printStackTrace();
			}
		}

	}
	@Override
	public void destroy() {
		server.close();
		super.destroy();
	}
	
	private void createLight() {
        Vector3f direction = new Vector3f(0.5f, -0.7f, -1).normalizeLocal();
        DirectionalLight dl = new DirectionalLight();
        dl.setDirection(direction);
        dl.setColor(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f));
        rootNode.addLight(dl);
        
        Vector3f direction2 = new Vector3f(-0.5f, -0.7f, 0.5f).normalizeLocal();
        DirectionalLight d2 = new DirectionalLight();
        d2.setDirection(direction2);
        d2.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1.0f));
        rootNode.addLight(d2);
    }
	
	private void setupFilter() {
        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
        BloomFilter bloom = new BloomFilter(BloomFilter.GlowMode.Objects);
        fpp.addFilter(bloom);
        viewPort.addProcessor(fpp);
    }
}//end of class